import {
  formatTime,
  ajax
} from '../../utils/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: '默认排序', value: 0 },
      { text: '按发布时间升序', value: 1 },
      { text: '按发布时间降序', value: 2 },
    ],
    option2: [
      { text: '所有系统', value: 'a' },
      { text: 'HarmonyOS', value: 'e' },
      { text: 'openEuler', value: 'f' },
      { text: 'MacOS', value: 'b' },
      { text: 'Linux', value: 'c' },
      { text: '其他', value: 'd' },
    ],
    value:'',
    value1: 0,
    value2:'a',
    list1:[],
  },

  changeSorting(e){
    if(e.detail === 0){
      this.setData({
        list1:[]
      })
      this.getArticle()
    }else if(e.detail === 1){
      this.setData({
        list1:[]
      })
      this.getArticleByTimeASC()
    }else{
      this.setData({
        list1:[]
      })
      this.getArticleByTimeDESC()
    }
  },

  changeSystem(e){
    if(e.detail === 'a'){
      this.setData({
        list1:[]
      })
      this.getArticle()
    }else if(e.detail === 'b'){
      this.setData({
        list1:[]
      })
      this.getArticleMacOS()
    }else if(e.detail === 'c'){
      this.setData({
        list1:[]
      })
      this.getArticleLinux()
    }else if(e.detail === 'd'){
      this.setData({
        list1:[]
      })
      this.getArticleOtherSystem()
    }else if(e.detail === 'e'){
      this.setData({
        list1:[]
      })
      this.getArticleHarmonyOS()
    }else if(e.detail === 'f'){
      this.setData({
        list1:[]
      })
      this.getArticleOpenEuler()
    }
  },

  onChange(e){
    this.setData({
      value: e.detail
    })
  },

  onClick(){
    if(this.data.value === ''){
      wx.showToast({
        title: '请输入想查询的文章名称',
        icon:'none'
      })
    }else{
      let name = this.data.value;
      wx.navigateTo({
        url: '../search/search?name='+name,
        success:()=>{
          this.setData({
            value:''
          })
        }
      })
    }
  },

  onSearch(){
    if(this.data.value === ''){
      wx.showToast({
        title: '请输入想查询的文章名称',
        icon:'none'
      })
    }else{
      let name = this.data.value;
      wx.navigateTo({
        url: '../search/search?name='+name,
        success:()=>{
          this.setData({
            value:''
          })
        }
      })
    }
  },

  getIscollected: async function(e) {
    const {
      info
    } = e.currentTarget.dataset;
    const {
      title,
      author,
      cover,
      link,
      aboutSystem,
      _id,
      updateTime
    } = info
    let iscollected = e.detail;
    this.setData({
      iscollected: e.detail
    })
    if (iscollected) {
      const params2 = {
        title,
          author,
          cover,
          link,
          aboutSystem,
          id: _id,
          updateTime,
          openid:wx.getStorageSync('openid')
      }
      const result1 = await ajax('/toCollection','POST',params2);
      const {
        data
      } = result1;
      if (data === "success") {
        wx.showToast({
          title: '收藏成功！',
          icon: 'none'
        })
      }
    } else {
      //已收藏，想取消
      const params3 = {
        id:info.id,
          openid:wx.getStorageSync('openid')
      }
      const result2 = await ajax('/cancelCollection','POST',params3);
      const{data} = result2;
          if(data === "success"){
            wx.showToast({
              title: '取消成功',
              icon:"none"
            })
          }
    }
  },

  toDetail(e) {
    const {
      info
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../articleDetail/articleDetail?info=` + encodeURIComponent(JSON.stringify(info)),
    })
  },

  getArticle: async function (options){
    const result = await ajax('/getArticle','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleByTimeDESC: async function (options){
    const result = await ajax('/getArticleByTimeDESC','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleByTimeASC: async function (options){
    const result = await ajax('/getArticleByTimeASC','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleMacOS: async function (options){
    const result = await ajax('/getArticleMacOS','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleLinux: async function (options){
    const result = await ajax('/getArticleLinux','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleOtherSystem: async function (options){
    const result = await ajax('/getArticleOtherSystem','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleOpenEuler: async function (options){
    const result = await ajax('/getArticleOpenEuler','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  getArticleHarmonyOS: async function (options){
    const result = await ajax('/getArticleHarmonyOS','GET','')
    const {
      data
    } = result;
    this.setData({
      list1: data.map(item => {
        return {
          ...item,
          id:item._id,
          updateTime: formatTime(item.updateTime)
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(){
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list1:[]
    })
    this.getArticle();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})